package cn.yuan.xiaoyu.testmodule.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cn.yuan.xiaoyu.R;
import cn.yuan.xiaoyu.commonmodule.utils.L;
import cn.yuan.xiaoyu.testmodule.bean.EventProgressBarBean;

/**
 * Created by yukuoyuan on 2017/11/7.
 * 这是一个自定义的进度条的view
 */
public class EventProgressBarView extends View {
    private Context context;
    private Paint textpaint;
    private List<EventProgressBarBean> eventProgressBarBeanList = new ArrayList<>();
    /**
     * 宽度
     */
    private float width;
    private float height;
    /**
     * 字体选中的颜色
     */
    private int mTextCheckedColor = Color.parseColor("#605bf5");
    /**
     * 字体未选中的颜色
     */
    private int mTextUnCheckedColor = Color.parseColor("#999999");
    /**
     * 圆选中的颜色
     */
    private int mCircleCheckedColor = Color.parseColor("#605bf5");
    /**
     * 圆未选中的颜色
     */
    private int mCircleUnCheckedColor = Color.parseColor("#999999");
    /**
     * 横线选中的颜色
     */
    private int mLineCheckedColor = Color.parseColor("#605bf5");
    /**
     * 横线未选中的颜色
     */
    private int mLineUnCheckedColor = Color.parseColor("#999999");
    /**
     * 事件字体大小
     */
    private int mEventTextSize = 32;
    /**
     * 圆半径大小
     */
    private int mCircleRadiusSize = 10;

    public EventProgressBarView(Context context) {
        this(context, null);
    }

    public EventProgressBarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EventProgressBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


    /**
     * 设置数据源
     *
     * @param eventProgressBarBeanList
     */
    public void setProgessList(List<EventProgressBarBean> eventProgressBarBeanList) {
        this.eventProgressBarBeanList = eventProgressBarBeanList;
        postInvalidate();
    }

    /**
     * 初始化
     *
     * @param context
     * @param attrs
     */
    public void init(Context context, @Nullable AttributeSet attrs) {
        this.context = context;
        /**
         * 初始化字体的画笔
         */
        textpaint = new TextPaint();
        textpaint.setStyle(Paint.Style.FILL);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.WeekView, 0, 0);

        try {
            /**
             * 事件字体选中的颜色
             */
            mTextCheckedColor = typedArray.getColor(R.styleable.ProgressBarEventView_textCheckedColor, mTextCheckedColor);
            /**
             * 事件字体未选中的颜色
             */
            mTextUnCheckedColor = typedArray.getColor(R.styleable.ProgressBarEventView_textUnCheckedColor, mTextUnCheckedColor);
            /**
             * 圆选中的颜色
             */
            mCircleCheckedColor = typedArray.getColor(R.styleable.ProgressBarEventView_circleCheckedColor, mCircleCheckedColor);
            /**
             * 圆未选中的颜色
             */
            mCircleUnCheckedColor = typedArray.getColor(R.styleable.ProgressBarEventView_circleUnCheckedColor, mCircleUnCheckedColor);
            /**
             * 线选中的颜色
             */
            mLineCheckedColor = typedArray.getColor(R.styleable.ProgressBarEventView_lineCheckedColor, mLineCheckedColor);
            /**
             * 线未选中的颜色
             */
            mLineUnCheckedColor = typedArray.getColor(R.styleable.ProgressBarEventView_lineUnCheckedColor, mLineUnCheckedColor);
            /**
             * 事件字体的大小
             */
            mEventTextSize = typedArray.getDimensionPixelSize(R.styleable.ProgressBarEventView_pbeventTextSize, mEventTextSize);
            /**
             * 圆半径大小
             */
            mCircleRadiusSize = typedArray.getDimensionPixelSize(R.styleable.ProgressBarEventView_circleRadiusSize, mCircleRadiusSize);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.width = w;
        this.height = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (eventProgressBarBeanList == null && eventProgressBarBeanList.size() == 0) {
            return;
        }
        /**
         * 不循环最后一个条目
         */
        float size = eventProgressBarBeanList.size() - 1;
        /**
         * 横线距离最上边的距离
         */
        float lineY = height - mCircleRadiusSize;
        /**
         * 循环进度集合
         */
        for (int b = 0; b <= size; b++) {
            /**
             * 每个点距离左边的位置
             */
            float circleX = width * (b / size);
            /**
             * 线开始的位置
             */
            float lineStartX = width * (b / size) + mCircleRadiusSize * 2;
            /**
             * 线结束的位置
             */
            float lineEndX = width * ((b + 1F) / size) - mCircleRadiusSize * 2;
            /**
             * 获取测量的字体的宽和高
             */
            Rect rect = new Rect();
            textpaint.getTextBounds(eventProgressBarBeanList.get(b).name, 0, eventProgressBarBeanList.get(b).name.length(), rect);
            if (b == 0) {
                /**
                 * 绘制线
                 */
                if (b + 1 <= size) {
                    //是否选中
                    if (eventProgressBarBeanList.get(b + 1).isChecked) {
                        textpaint.setColor(mTextCheckedColor);
                    } else {
                        textpaint.setColor(mTextUnCheckedColor);
                    }
                }
                canvas.drawLine(lineStartX + rect.width() / 2, lineY, lineEndX, lineY, textpaint);

                /**
                 * 第一个点距左的距离
                 */
                circleX = rect.width() / 2;
                textpaint.setTextSize(mEventTextSize);

                //是否选中
                if (eventProgressBarBeanList.get(b).isChecked) {
                    textpaint.setColor(mTextCheckedColor);
                } else {
                    textpaint.setColor(mTextUnCheckedColor);
                }
                //第一个字体
                canvas.drawText(eventProgressBarBeanList.get(b).name, 0, rect.height(), textpaint);


            } else if (b == size) {
                /**
                 * 最后一个点距左的距离
                 */
                circleX = circleX - rect.width() / 2;
                textpaint.setTextSize(mEventTextSize);

                //是否选中
                if (eventProgressBarBeanList.get(b).isChecked) {
                    textpaint.setColor(mTextCheckedColor);
                } else {
                    textpaint.setColor(mTextUnCheckedColor);
                }
                //最后一个字体
                canvas.drawText(eventProgressBarBeanList.get(b).name, width - rect.width(), rect.height(), textpaint);
            } else {
                /**
                 * 绘制xain线
                 */
                if (b + 1 <= size) {
                    //是否选中
                    if (eventProgressBarBeanList.get(b + 1).isChecked) {
                        textpaint.setColor(mTextCheckedColor);
                    } else {
                        textpaint.setColor(mTextUnCheckedColor);
                    }
                }
                if (b == size - 1) {
                    canvas.drawLine(lineStartX, lineY, lineEndX - rect.width() / 2, lineY, textpaint);
                    L.d("绘制", "最后一条线");
                } else {
                    canvas.drawLine(lineStartX, lineY, lineEndX, lineY, textpaint);
                    L.d("绘制", "中间的线");
                }
                textpaint.setTextSize(mEventTextSize);
                //是否选中
                if (eventProgressBarBeanList.get(b).isChecked) {
                    textpaint.setColor(mTextCheckedColor);
                } else {
                    textpaint.setColor(mTextUnCheckedColor);
                }
                //中间的字体
                canvas.drawText(eventProgressBarBeanList.get(b).name, circleX - rect.width() / 2, rect.height(), textpaint);

            }
            canvas.drawCircle(circleX, lineY, mCircleRadiusSize, textpaint);
        }


    }

}
